{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组操作\n",
    "\n",
    "## 更改形状\n",
    "\n",
    "在对数组进行操作时，为了满足格式和计算的要求通常会改变其形状。\n",
    "\n",
    "- `numpy.ndarray.shape`表示数组的维度，返回一个元组，这个元组的长度就是维度的数目，即 `ndim` 属性(秩)。\n",
    "\n",
    "【例】通过修改 shape 属性来改变数组的形状。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 9, 4, 5, 6, 7, 8])\n",
    "print(x.shape)  # (8,)\n",
    "x.shape = [2, 4]\n",
    "print(x)\n",
    "# [[1 2 9 4]\n",
    "#  [5 6 7 8]]\n",
    "```\n",
    "\n",
    "- `numpy.ndarray.flat` 将数组转换为一维的迭代器，可以用for访问数组每一个元素。\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = x.flat\n",
    "print(y)\n",
    "# <numpy.flatiter object at 0x0000020F9BA10C60>\n",
    "for i in y:\n",
    "    print(i, end=' ')\n",
    "# 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35\n",
    "\n",
    "y[3] = 0\n",
    "print(end='\\n')\n",
    "print(x)\n",
    "# [[11 12 13  0 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]\n",
    "#  [26 27 28 29 30]\n",
    "#  [31 32 33 34 35]]\n",
    "```\n",
    "\n",
    "- `numpy.ndarray.flatten([order='C'])` 将数组的副本转换为一维数组，并返回。\n",
    "    - order：'C' -- 按行，'F' -- 按列，'A' -- 原顺序，'k' -- 元素在内存中的出现顺序。(简记)\n",
    "    - order：{'C / F，'A，K}，可选使用此索引顺序读取a的元素。'C'意味着以行大的C风格顺序对元素进行索引，最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素，其中第一个索引变化最快，最后一个索引变化最快。请注意，'C'和'F'选项不考虑基础数组的内存布局，仅引用轴索引的顺序.A'表示如果a为Fortran，则以类似Fortran的索引顺序读取元素在内存中连续，否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素，但步幅为负时反转数据除外。默认情况下，使用Cindex顺序。\n",
    "\n",
    "【例】`flatten()`函数返回的是拷贝。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = x.flatten()\n",
    "print(y)\n",
    "# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34\n",
    "#  35]\n",
    "\n",
    "y[3] = 0\n",
    "print(x)\n",
    "# [[11 12 13 14 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]\n",
    "#  [26 27 28 29 30]\n",
    "#  [31 32 33 34 35]]\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "\n",
    "y = x.flatten(order='F')\n",
    "print(y)\n",
    "# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30\n",
    "#  35]\n",
    "\n",
    "y[3] = 0\n",
    "print(x)\n",
    "# [[11 12 13 14 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]\n",
    "#  [26 27 28 29 30]\n",
    "#  [31 32 33 34 35]]\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "- `numpy.ravel(a, order='C')`Return a contiguous flattened array.\n",
    "\n",
    "【例】`ravel()`返回的是视图。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "y = np.ravel(x)\n",
    "print(y)\n",
    "# [11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34\n",
    "#  35]\n",
    "\n",
    "y[3] = 0\n",
    "print(x)\n",
    "# [[11 12 13  0 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]\n",
    "#  [26 27 28 29 30]\n",
    "#  [31 32 33 34 35]]\n",
    "\n",
    "【例】order=F 就是拷贝\n",
    "\n",
    "x = np.array([[11, 12, 13, 14, 15],\n",
    "              [16, 17, 18, 19, 20],\n",
    "              [21, 22, 23, 24, 25],\n",
    "              [26, 27, 28, 29, 30],\n",
    "              [31, 32, 33, 34, 35]])\n",
    "\n",
    "y = np.ravel(x, order='F')\n",
    "print(y)\n",
    "# [11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30\n",
    "#  35]\n",
    "\n",
    "y[3] = 0\n",
    "print(x)\n",
    "# [[11 12 13 14 15]\n",
    "#  [16 17 18 19 20]\n",
    "#  [21 22 23 24 25]\n",
    "#  [26 27 28 29 30]\n",
    "#  [31 32 33 34 35]]\n",
    "```\n",
    "\n",
    "- `numpy.reshape(a, newshape[, order='C'])`在不更改数据的情况下为数组赋予新的形状。\n",
    "\n",
    "【例】`reshape()`函数当参数`newshape = [rows,-1]`时，将根据行数自动确定列数。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.arange(12)\n",
    "y = np.reshape(x, [3, 4])\n",
    "print(y.dtype)  # int32\n",
    "print(y)\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 4  5  6  7]\n",
    "#  [ 8  9 10 11]]\n",
    "\n",
    "y = np.reshape(x, [3, -1])\n",
    "print(y)\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 4  5  6  7]\n",
    "#  [ 8  9 10 11]]\n",
    "\n",
    "y = np.reshape(x,[-1,3])\n",
    "print(y)\n",
    "# [[ 0  1  2]\n",
    "#  [ 3  4  5]\n",
    "#  [ 6  7  8]\n",
    "#  [ 9 10 11]]\n",
    "\n",
    "y[0, 1] = 10\n",
    "print(x)\n",
    "# [ 0 10  2  3  4  5  6  7  8  9 10 11]（改变x去reshape后y中的值，x对应元素也改变）\n",
    "```\n",
    "\n",
    "【例】`reshape()`函数当参数`newshape = -1`时，表示将数组降为一维。\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.random.randint(12, size=[2, 2, 3])\n",
    "print(x)\n",
    "# [[[11  9  1]\n",
    "#   [ 1 10  3]]\n",
    "# \n",
    "#  [[ 0  6  1]\n",
    "#   [ 4 11  3]]]\n",
    "y = np.reshape(x, -1)\n",
    "print(y)\n",
    "# [11  9  1  1 10  3  0  6  1  4 11  3]\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "## 数组转置\n",
    "\n",
    "\n",
    "- `numpy.transpose(a, axes=None)` Permute the dimensions of an array.\n",
    "- `numpy.ndarray.T` Same as `self.transpose()`, except that self is returned if `self.ndim < 2`.\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.random.rand(5, 5) * 10\n",
    "x = np.around(x, 2)\n",
    "print(x)\n",
    "# [[6.74 8.46 6.74 5.45 1.25]\n",
    "#  [3.54 3.49 8.62 1.94 9.92]\n",
    "#  [5.03 7.22 1.6  8.7  0.43]\n",
    "#  [7.5  7.31 5.69 9.67 7.65]\n",
    "#  [1.8  9.52 2.78 5.87 4.14]]\n",
    "y = x.T\n",
    "print(y)\n",
    "# [[6.74 3.54 5.03 7.5  1.8 ]\n",
    "#  [8.46 3.49 7.22 7.31 9.52]\n",
    "#  [6.74 8.62 1.6  5.69 2.78]\n",
    "#  [5.45 1.94 8.7  9.67 5.87]\n",
    "#  [1.25 9.92 0.43 7.65 4.14]]\n",
    "y = np.transpose(x)\n",
    "print(y)\n",
    "# [[6.74 3.54 5.03 7.5  1.8 ]\n",
    "#  [8.46 3.49 7.22 7.31 9.52]\n",
    "#  [6.74 8.62 1.6  5.69 2.78]\n",
    "#  [5.45 1.94 8.7  9.67 5.87]\n",
    "#  [1.25 9.92 0.43 7.65 4.14]]\n",
    "```\n",
    "\n",
    "\n",
    "## 更改维度\n",
    "\n",
    "当创建一个数组之后，还可以给它增加一个维度，这在矩阵计算中经常会用到。\n",
    "\n",
    "- `numpy.newaxis = None` `None`的别名，对索引数组很有用。\n",
    "\n",
    "【例】很多工具包在进行计算时都会先判断输入数据的维度是否满足要求，如果输入数据达不到指定的维度时，可以使用`newaxis`参数来增加一个维度。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 9, 4, 5, 6, 7, 8])\n",
    "print(x.shape)  # (8,)\n",
    "print(x)  # [1 2 9 4 5 6 7 8]\n",
    "\n",
    "y = x[np.newaxis, :]\n",
    "print(y.shape)  # (1, 8)\n",
    "print(y)  # [[1 2 9 4 5 6 7 8]]\n",
    "\n",
    "y = x[:, np.newaxis]\n",
    "print(y.shape)  # (8, 1)\n",
    "print(y)\n",
    "# [[1]\n",
    "#  [2]\n",
    "#  [9]\n",
    "#  [4]\n",
    "#  [5]\n",
    "#  [6]\n",
    "#  [7]\n",
    "#  [8]]\n",
    "```\n",
    "\n",
    "- `numpy.squeeze(a, axis=None)` 从数组的形状中删除单维度条目，即把shape中为1的维度去掉。\n",
    "    - `a`表示输入的数组；\n",
    "    - `axis`用于指定需要删除的维度，但是指定的维度必须为单维度，否则将会报错；\n",
    "\n",
    "在机器学习和深度学习中，通常算法的结果是可以表示向量的数组（即包含两对或以上的方括号形式[[]]），如果直接利用这个数组进行画图可能显示界面为空（见后面的示例）。我们可以利用`squeeze()`函数将表示向量的数组转换为秩为1的数组，这样利用 matplotlib 库函数画图时，就可以正常的显示结果了。\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.arange(10)\n",
    "print(x.shape)  # (10,)\n",
    "x = x[np.newaxis, :]\n",
    "print(x.shape)  # (1, 10)\n",
    "y = np.squeeze(x)\n",
    "print(y.shape)  # (10,)\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[[0], [1], [2]]])\n",
    "print(x.shape)  # (1, 3, 1)\n",
    "print(x)\n",
    "# [[[0]\n",
    "#   [1]\n",
    "#   [2]]]\n",
    "\n",
    "y = np.squeeze(x)\n",
    "print(y.shape)  # (3,)\n",
    "print(y)  # [0 1 2]\n",
    "\n",
    "y = np.squeeze(x, axis=0)\n",
    "print(y.shape)  # (3, 1)\n",
    "print(y)\n",
    "# [[0]\n",
    "#  [1]\n",
    "#  [2]]\n",
    "\n",
    "y = np.squeeze(x, axis=2)\n",
    "print(y.shape)  # (1, 3)\n",
    "print(y)  # [[0 1 2]]\n",
    "\n",
    "y = np.squeeze(x, axis=1)\n",
    "# ValueError: cannot select an axis to squeeze out which has size not equal to one\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.array([[1, 4, 9, 16, 25]])\n",
    "print(x.shape)  # (1, 5)\n",
    "plt.plot(x)\n",
    "plt.show()\n",
    "```\n",
    "![](https://img-blog.csdnimg.cn/20200528095957317.png)\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x = np.array([[1, 4, 9, 16, 25]])\n",
    "x = np.squeeze(x)\n",
    "print(x.shape)  # (5, )\n",
    "plt.plot(x)\n",
    "plt.show()\n",
    "```\n",
    "\n",
    "![](https://img-blog.csdnimg.cn/20200528100221464.png)\n",
    "\n",
    "## 数组组合\n",
    "\n",
    "如果要将两份数据组合到一起，就需要拼接操作。\n",
    "\n",
    "- `numpy.concatenate((a1, a2, ...), axis=0, out=None)` Join a sequence of arrays along an existing axis.\n",
    "\n",
    "【例】连接沿现有轴的数组序列（原来x，y都是一维的，拼接后的结果也是一维的）。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3])\n",
    "y = np.array([7, 8, 9])\n",
    "z = np.concatenate([x, y])\n",
    "print(z)\n",
    "# [1 2 3 7 8 9]\n",
    "\n",
    "z = np.concatenate([x, y], axis=0)\n",
    "print(z)\n",
    "# [1 2 3 7 8 9]\n",
    "```\n",
    "\n",
    "【例】原来x，y都是二维的，拼接后的结果也是二维的。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3]).reshape(1, 3)\n",
    "y = np.array([7, 8, 9]).reshape(1, 3)\n",
    "z = np.concatenate([x, y])\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 7  8  9]]\n",
    "z = np.concatenate([x, y], axis=0)\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 7  8  9]]\n",
    "z = np.concatenate([x, y], axis=1)\n",
    "print(z)\n",
    "# [[ 1  2  3  7  8  9]]\n",
    "```\n",
    "\n",
    "【例】x，y在原来的维度上进行拼接。\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "z = np.concatenate([x, y])\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 4  5  6]\n",
    "#  [ 7  8  9]\n",
    "#  [10 11 12]]\n",
    "z = np.concatenate([x, y], axis=0)\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 4  5  6]\n",
    "#  [ 7  8  9]\n",
    "#  [10 11 12]]\n",
    "z = np.concatenate([x, y], axis=1)\n",
    "print(z)\n",
    "# [[ 1  2  3  7  8  9]\n",
    "#  [ 4  5  6 10 11 12]]\n",
    "```\n",
    "\n",
    "- `numpy.stack(arrays, axis=0, out=None)`Join a sequence of arrays along a new axis.\n",
    "\n",
    "\n",
    "【例】沿着新的轴加入一系列数组（stack为增加维度的拼接）。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3])\n",
    "y = np.array([7, 8, 9])\n",
    "z = np.stack([x, y])\n",
    "print(z.shape)  # (2, 3)\n",
    "print(z)\n",
    "# [[1 2 3]\n",
    "#  [7 8 9]]\n",
    "\n",
    "z = np.stack([x, y], axis=1)\n",
    "print(z.shape)  # (3, 2)\n",
    "print(z)\n",
    "# [[1 7]\n",
    "#  [2 8]\n",
    "#  [3 9]]\n",
    "```\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3]).reshape(1, 3)\n",
    "y = np.array([7, 8, 9]).reshape(1, 3)\n",
    "z = np.stack([x, y])\n",
    "print(z.shape)  # (2, 1, 3)\n",
    "print(z)\n",
    "# [[[1 2 3]]\n",
    "#\n",
    "#  [[7 8 9]]]\n",
    "\n",
    "z = np.stack([x, y], axis=1)\n",
    "print(z.shape)  # (1, 2, 3)\n",
    "print(z)\n",
    "# [[[1 2 3]\n",
    "#   [7 8 9]]]\n",
    "\n",
    "z = np.stack([x, y], axis=2)\n",
    "print(z.shape)  # (1, 3, 2)\n",
    "print(z)\n",
    "# [[[1 7]\n",
    "#   [2 8]\n",
    "#   [3 9]]]\n",
    "```\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "z = np.stack([x, y])\n",
    "print(z.shape)  # (2, 2, 3)\n",
    "print(z)\n",
    "# [[[ 1  2  3]\n",
    "#   [ 4  5  6]]\n",
    "# \n",
    "#  [[ 7  8  9]\n",
    "#   [10 11 12]]]\n",
    "\n",
    "z = np.stack([x, y], axis=1)\n",
    "print(z.shape)  # (2, 2, 3)\n",
    "print(z)\n",
    "# [[[ 1  2  3]\n",
    "#   [ 7  8  9]]\n",
    "# \n",
    "#  [[ 4  5  6]\n",
    "#   [10 11 12]]]\n",
    "\n",
    "z = np.stack([x, y], axis=2)\n",
    "print(z.shape)  # (2, 3, 2)\n",
    "print(z)\n",
    "# [[[ 1  7]\n",
    "#   [ 2  8]\n",
    "#   [ 3  9]]\n",
    "# \n",
    "#  [[ 4 10]\n",
    "#   [ 5 11]\n",
    "#   [ 6 12]]]\n",
    "```\n",
    "\n",
    "\n",
    "- `numpy.vstack(tup)`Stack arrays in sequence vertically (row wise).\n",
    "- `numpy.hstack(tup)`Stack arrays in sequence horizontally (column wise). \n",
    "\n",
    "\n",
    "【例】一维的情况。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3])\n",
    "y = np.array([7, 8, 9])\n",
    "z = np.vstack((x, y))\n",
    "print(z.shape)  # (2, 3)\n",
    "print(z)\n",
    "# [[1 2 3]\n",
    "#  [7 8 9]]\n",
    "\n",
    "z = np.stack([x, y])\n",
    "print(z.shape)  # (2, 3)\n",
    "print(z)\n",
    "# [[1 2 3]\n",
    "#  [7 8 9]]\n",
    "\n",
    "z = np.hstack((x, y))\n",
    "print(z.shape)  # (6,)\n",
    "print(z)\n",
    "# [1  2  3  7  8  9]\n",
    "\n",
    "z = np.concatenate((x, y))\n",
    "print(z.shape)  # (6,)\n",
    "print(z)  # [1 2 3 7 8 9]\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "【例】二维的情况。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([1, 2, 3]).reshape(1, 3)\n",
    "y = np.array([7, 8, 9]).reshape(1, 3)\n",
    "z = np.vstack((x, y))\n",
    "print(z.shape)  # (2, 3)\n",
    "print(z)\n",
    "# [[1 2 3]\n",
    "#  [7 8 9]]\n",
    "\n",
    "z = np.concatenate((x, y), axis=0)\n",
    "print(z.shape)  # (2, 3)\n",
    "print(z)\n",
    "# [[1 2 3]\n",
    "#  [7 8 9]]\n",
    "\n",
    "z = np.hstack((x, y))\n",
    "print(z.shape)  # (1, 6)\n",
    "print(z)\n",
    "# [[ 1  2  3  7  8  9]]\n",
    "\n",
    "z = np.concatenate((x, y), axis=1)\n",
    "print(z.shape)  # (1, 6)\n",
    "print(z)\n",
    "# [[1 2 3 7 8 9]]\n",
    "```\n",
    "\n",
    "【例】二维的情况。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "z = np.vstack((x, y))\n",
    "print(z.shape)  # (4, 3)\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 4  5  6]\n",
    "#  [ 7  8  9]\n",
    "#  [10 11 12]]\n",
    "\n",
    "z = np.concatenate((x, y), axis=0)\n",
    "print(z.shape)  # (4, 3)\n",
    "print(z)\n",
    "# [[ 1  2  3]\n",
    "#  [ 4  5  6]\n",
    "#  [ 7  8  9]\n",
    "#  [10 11 12]]\n",
    "\n",
    "z = np.hstack((x, y))\n",
    "print(z.shape)  # (2, 6)\n",
    "print(z)\n",
    "# [[ 1  2  3  7  8  9]\n",
    "#  [ 4  5  6 10 11 12]]\n",
    "\n",
    "z = np.concatenate((x, y), axis=1)\n",
    "print(z.shape)  # (2, 6)\n",
    "print(z)\n",
    "# [[ 1  2  3  7  8  9]\n",
    "#  [ 4  5  6 10 11 12]]\n",
    "```\n",
    "\n",
    "`hstack(),vstack()`分别表示水平和竖直的拼接方式。在数据维度等于1时，比较特殊。而当维度大于或等于2时，它们的作用相当于`concatenate`，用于在已有轴上进行操作。\n",
    "\n",
    "【例】\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "a = np.hstack([np.array([1, 2, 3, 4]), 5])\n",
    "print(a)  # [1 2 3 4 5]\n",
    "\n",
    "a = np.concatenate([np.array([1, 2, 3, 4]), 5])\n",
    "print(a)\n",
    "# all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)\n",
    "```\n",
    "\n",
    "\n",
    "## 数组拆分\n",
    "\n",
    "- `numpy.split(ary, indices_or_sections, axis=0)` Split an array into multiple sub-arrays as views into ary.\n",
    "\n",
    "【例】拆分数组。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14],\n",
    "              [16, 17, 18, 19],\n",
    "              [21, 22, 23, 24]])\n",
    "y = np.split(x, [1, 3])\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],\n",
    "#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]\n",
    "\n",
    "y = np.split(x, [1, 3], axis=1)\n",
    "print(y)\n",
    "# [array([[11],\n",
    "#        [16],\n",
    "#        [21]]), array([[12, 13],\n",
    "#        [17, 18],\n",
    "#        [22, 23]]), array([[14],\n",
    "#        [19],\n",
    "#        [24]])]\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "- `numpy.vsplit(ary, indices_or_sections)` Split an array into multiple sub-arrays vertically (row-wise).\n",
    "\n",
    "【例】垂直切分是把数组按照高度切分\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14],\n",
    "              [16, 17, 18, 19],\n",
    "              [21, 22, 23, 24]])\n",
    "y = np.vsplit(x, 3)\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]\n",
    "\n",
    "y = np.split(x, 3)\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19]]), array([[21, 22, 23, 24]])]\n",
    "\n",
    "\n",
    "y = np.vsplit(x, [1])\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],\n",
    "#        [21, 22, 23, 24]])]\n",
    "\n",
    "y = np.split(x, [1])\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],\n",
    "#        [21, 22, 23, 24]])]\n",
    "\n",
    "\n",
    "y = np.vsplit(x, [1, 3])\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],\n",
    "#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]\n",
    "y = np.split(x, [1, 3], axis=0)\n",
    "print(y)\n",
    "# [array([[11, 12, 13, 14]]), array([[16, 17, 18, 19],\n",
    "#        [21, 22, 23, 24]]), array([], shape=(0, 4), dtype=int32)]\n",
    "```\n",
    "\n",
    "- `numpy.hsplit(ary, indices_or_sections)` Split an array into multiple sub-arrays horizontally (column-wise).\n",
    "\n",
    "\n",
    "【例】水平切分是把数组按照宽度切分。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[11, 12, 13, 14],\n",
    "              [16, 17, 18, 19],\n",
    "              [21, 22, 23, 24]])\n",
    "y = np.hsplit(x, 2)\n",
    "print(y)\n",
    "# [array([[11, 12],\n",
    "#        [16, 17],\n",
    "#        [21, 22]]), array([[13, 14],\n",
    "#        [18, 19],\n",
    "#        [23, 24]])]\n",
    "\n",
    "y = np.split(x, 2, axis=1)\n",
    "print(y)\n",
    "# [array([[11, 12],\n",
    "#        [16, 17],\n",
    "#        [21, 22]]), array([[13, 14],\n",
    "#        [18, 19],\n",
    "#        [23, 24]])]\n",
    "\n",
    "y = np.hsplit(x, [3])\n",
    "print(y)\n",
    "# [array([[11, 12, 13],\n",
    "#        [16, 17, 18],\n",
    "#        [21, 22, 23]]), array([[14],\n",
    "#        [19],\n",
    "#        [24]])]\n",
    "\n",
    "y = np.split(x, [3], axis=1)\n",
    "print(y)\n",
    "# [array([[11, 12, 13],\n",
    "#        [16, 17, 18],\n",
    "#        [21, 22, 23]]), array([[14],\n",
    "#        [19],\n",
    "#        [24]])]\n",
    "\n",
    "y = np.hsplit(x, [1, 3])\n",
    "print(y)\n",
    "# [array([[11],\n",
    "#        [16],\n",
    "#        [21]]), array([[12, 13],\n",
    "#        [17, 18],\n",
    "#        [22, 23]]), array([[14],\n",
    "#        [19],\n",
    "#        [24]])]\n",
    "\n",
    "y = np.split(x, [1, 3], axis=1)\n",
    "print(y)\n",
    "# [array([[11],\n",
    "#        [16],\n",
    "#        [21]]), array([[12, 13],\n",
    "#        [17, 18],\n",
    "#        [22, 23]]), array([[14],\n",
    "#        [19],\n",
    "#        [24]])]\n",
    "```\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## 数组平铺\n",
    "\n",
    "- `numpy.tile(A, reps)` Construct an array by repeating A the number of times given by reps.\n",
    "\n",
    "`tile`是瓷砖的意思，顾名思义，这个函数就是把数组像瓷砖一样铺展开来。\n",
    "\n",
    "【例】将原矩阵横向、纵向地复制。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.array([[1, 2], [3, 4]])\n",
    "print(x)\n",
    "# [[1 2]\n",
    "#  [3 4]]\n",
    "\n",
    "y = np.tile(x, (1, 3))\n",
    "print(y)\n",
    "# [[1 2 1 2 1 2]\n",
    "#  [3 4 3 4 3 4]]\n",
    "\n",
    "y = np.tile(x, (3, 1))\n",
    "print(y)\n",
    "# [[1 2]\n",
    "#  [3 4]\n",
    "#  [1 2]\n",
    "#  [3 4]\n",
    "#  [1 2]\n",
    "#  [3 4]]\n",
    "\n",
    "y = np.tile(x, (3, 3))\n",
    "print(y)\n",
    "# [[1 2 1 2 1 2]\n",
    "#  [3 4 3 4 3 4]\n",
    "#  [1 2 1 2 1 2]\n",
    "#  [3 4 3 4 3 4]\n",
    "#  [1 2 1 2 1 2]\n",
    "#  [3 4 3 4 3 4]]\n",
    "```\n",
    "\n",
    "- `numpy.repeat(a, repeats, axis=None)` Repeat elements of an array.\n",
    "    - `axis=0`，沿着y轴复制，实际上增加了行数。\n",
    "    - `axis=1`，沿着x轴复制，实际上增加了列数。\n",
    "    - `repeats`，可以为一个数，也可以为一个矩阵。\n",
    "    - `axis=None`时就会flatten当前矩阵，实际上就是变成了一个行向量。\n",
    "\n",
    "【例】重复数组的元素。\n",
    "```python\n",
    "import numpy as np\n",
    "\n",
    "x = np.repeat(3, 4)\n",
    "print(x)  # [3 3 3 3]\n",
    "\n",
    "x = np.array([[1, 2], [3, 4]])\n",
    "y = np.repeat(x, 2)\n",
    "print(y)\n",
    "# [1 1 2 2 3 3 4 4]\n",
    "\n",
    "y = np.repeat(x, 2, axis=0)\n",
    "print(y)\n",
    "# [[1 2]\n",
    "#  [1 2]\n",
    "#  [3 4]\n",
    "#  [3 4]]\n",
    "\n",
    "y = np.repeat(x, 2, axis=1)\n",
    "print(y)\n",
    "# [[1 1 2 2]\n",
    "#  [3 3 4 4]]\n",
    "\n",
    "y = np.repeat(x, [2, 3], axis=0)\n",
    "print(y)\n",
    "# [[1 2]\n",
    "#  [1 2]\n",
    "#  [3 4]\n",
    "#  [3 4]\n",
    "#  [3 4]]\n",
    "\n",
    "y = np.repeat(x, [2, 3], axis=1)\n",
    "print(y)\n",
    "# [[1 1 2 2 2]\n",
    "#  [3 3 4 4 4]]\n",
    "```\n",
    "\n",
    "---\n",
    "## 添加和删除元素\n",
    "\n",
    "- `numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None)` Find the unique elements of an array.\n",
    "    - return_index：the indices of the input array that give the unique values\n",
    "    - return_inverse：the indices of the unique array that reconstruct the input array\n",
    "    - return_counts：the number of times each unique value comes up in the input array\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "【例】查找数组的唯一元素。\n",
    "```python\n",
    "a=np.array([1,1,2,3,3,4,4])\n",
    "b=np.unique(a,return_counts=True)\n",
    "print(b[0][list(b[1]).index(1)])\n",
    "#2\n",
    "```\n",
    "\n",
    "---\n",
    "**参考文献**\n",
    "- https://blog.csdn.net/csdn15698845876/article/details/73380803\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-30T13:01:49.899843Z",
     "start_time": "2020-08-30T13:01:49.895853Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "a=np.array([1,1,2,3,3,4,4])\n",
    "b=np.unique(a,return_counts=True)\n",
    "print(b[0][list(b[1]).index(1)])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python35",
   "language": "python",
   "name": "python35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "307.193px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
